//
//  pictureViewController.m
//  iNews
//
//  Created by lanou3g on 14-12-15.
//  Copyright (c) 2014年 张在青. All rights reserved.
//

#import "pictureViewController.h"
#import "pictureView.h"

#import "ArchiveHandleSingle.h"
#import "PicTableViewCell.h"
#import "DetailArchiveModel.h"

#import "DetailPictureViewController.h"
#import "DetailPicView.h"
#import "DetailModel.h"

@interface pictureViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,retain)DetailArchiveModel  *amodel;

@end

@implementation pictureViewController

- (void)dealloc
{
    [_pic release];
    [_amodel release];
    
    
    [super dealloc];
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
      //  NSLog(@"%@", NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES));
       
        //反归档读数据
        [[ArchiveHandleSingle archiveSingle] searchPicture];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloadView) name:@"pictureArray" object:nil];
        
        self.pic = [[[pictureView alloc] initWithFrame:CGRectMake(0,89, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height - 89 - 44)] autorelease];
    }
    return self;
}


-(void)loadView{
    self.view = self.pic;
    
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    
    
    
    //设置代理
    self.pic.pctureTab.delegate = self;
    self.pic.pctureTab.dataSource = self;
    
    //注册cell
   
    [self.pic.pctureTab registerClass:[PicTableViewCell class] forCellReuseIdentifier:@"cell_id"];
    [self.pic.pctureTab registerClass:[UITableViewCell class] forCellReuseIdentifier:@"tableCell"];

    
    // Do any additional setup after loading the view.
}

//通知中心方法
- (void)reloadView
{
    
    [self.pic.pctureTab reloadData];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    
    if ([ArchiveHandleSingle archiveSingle].pictureArray.count == 0) {
        return 1;
    }else{
    
        return [ArchiveHandleSingle archiveSingle].pictureArray.count;
    }
    
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
    
}

//返回cell的方法
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if ([ArchiveHandleSingle archiveSingle].pictureArray.count == 0) {
        
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"tableCell" forIndexPath:indexPath];
        
        cell.textLabel.text = @"暂无收藏";
        
        return cell;
        
    }else{
        
        PicTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell_id" forIndexPath:indexPath];
    
        int t = [ArchiveHandleSingle archiveSingle].pictureArray.count;
       
        DetailArchiveModel *dam = [ArchiveHandleSingle archiveSingle].pictureArray[t - indexPath.row - 1];

//        //赋给自己的model
//        self.amodel = [[[DetailArchiveModel alloc] init] autorelease];
//        self.amodel = dam;
        
        if (dam.imageArray.count == 0) {
            cell.imV.image = [UIImage imageNamed:@"1.png"];
        }else{
            cell.imV.image = dam.imageArray[0];
        }
        cell.lab.text = dam.setname;
        NSString *s = [NSString stringWithFormat:@"已加载%lu张",(unsigned long)dam.imageArray.count];
        cell.numLab.text = s;

         return cell;
    }
}

//返回cell高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return 180;
}

//点击cell的方法
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    int t = [ArchiveHandleSingle archiveSingle].pictureArray.count;
    if (t) {
        
        DetailArchiveModel *m = [[ArchiveHandleSingle archiveSingle].pictureArray objectAtIndex:t - indexPath.row - 1];
        
        DetailPictureViewController *picVC = [[[DetailPictureViewController alloc] init] autorelease];
        picVC.temp = m.imageArray.count;
//        picVC.postStr = m.postid;
        //没网的情况
        [picVC.dPicView writeMessageDAMWithModel:m];
        
        picVC.im = [UIImage imageNamed:@"icon_star_full@2x.png"];
        [picVC.dPicView.rightButton setImage:[UIImage imageNamed:@"icon_star_full@2x.png"] forState:UIControlStateNormal];
        picVC.tempStr = m.postid;
       // [picVC passValue:m.postid];
        
        [self.navigationController presentViewController:picVC animated:YES completion:nil];

//        [picVC release];
    }else{
        
        UIAlertView *alv = [[UIAlertView alloc] initWithTitle:@"收藏的图片为0张" message:@"不能查看" delegate:nil cancelButtonTitle:nil otherButtonTitles:nil, nil];
        [alv show];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            
            [alv dismissWithClickedButtonIndex:0 animated:YES];
            [alv release];
        });
    }
    
}


//2.指定哪些行可以被编辑
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}
//3.确定编辑样式
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return _picStyle;
}
//4.完成编辑
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        
        //删除数据
        NSLog(@"删除图片");
        
        int count = [ArchiveHandleSingle archiveSingle].pictureArray.count;
        //删除沙盒数据
        if ([ArchiveHandleSingle archiveSingle].pictureArray.count == 0) {
            
            UIAlertView *av = [[UIAlertView alloc] initWithTitle:@"您还未收藏" message:nil delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil] ;
            [av show];
            
        }else{
            
            //删除数据
            //    [[ArchiveHandleSingle archiveSingle].articleArray removeObjectAtIndex:indexPath.row];
            
//            DetailArchiveModel *model = [ArchiveHandleSingle archiveSingle].pictureArray[count - indexPath.row - 1];
//            [[ArchiveHandleSingle archiveSingle] deletePictureView:model];
            
            NSString *documentPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
            NSString *filePath = [documentPath stringByAppendingString:@"/picture.plist"];
            
            //删除数据
            [[ArchiveHandleSingle archiveSingle].pictureArray removeObjectAtIndex:count - indexPath.row - 1];
            //重新归档
            [NSKeyedArchiver archiveRootObject:[ArchiveHandleSingle archiveSingle].pictureArray toFile:filePath];
            //跟新UI
            [self.pic.pctureTab reloadData];

        }
        
    }
    
}


/*

//给pictureView重写数据
- (void)writeMessageToPicViewController:(DetailPictureViewController *)p WithModel:(DetailArchiveModel *)m
{
    CGSize size = p.dPicView.scroll.contentSize;
    // CGPoint cen = self.scroll.center;
    
    p.dPicView.scroll.contentSize = CGSizeMake([UIScreen mainScreen].bounds.size.width * m.imageArray.count, size.height);
    
    for (int i = 0; i < m.imageArray.count; i++) {
        
        //附图片
        UIImageView *imV = [[[UIImageView alloc] initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width * i, 0, [UIScreen mainScreen].bounds.size.width,  CGRectGetHeight(p.dPicView.scroll.frame))] autorelease];
        
        imV.image = m.imageArray[i];
        [p.dPicView.scroll addSubview:imV];
        
     //   p.dPicView.textV.text = m.noteArray[i];
        
    }
    
    p.dPicView.label.text = m.setname;
    
}
*/
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
